package at.ac.uniklu.rallyemaster;
import java.util.ArrayList;
import java.util.LinkedList;

import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import at.ac.uniklu.rallyemaster.domain.TrackingPoint;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class TrackingMapOverlay extends ItemizedOverlay<OverlayItem>{
	private Paint textPaint;
	private Paint varPaint;
	private Typeface lcdFont;
	private Paint backPaint;
	private Paint circlePaint;
	private LinkedList<TrackingPoint> trackingPoints;
	private ArrayList<OverlayItem> mOverlays; 
	private Drawable marker;
	
	//Backgroud rectangles
	RectF backRect1 = new RectF();
	RectF backRect2 = new RectF();
	RectF backRect3 = new RectF();
	RectF backRect4 = new RectF();
	
	//Coordinates for the text
	int txtSpeedX;
	int txtSpeedY;
	int speedX;
	int speedy;
	
	int txtAvgSpeedX;
	int txtAvgSpeedY; 
	int avgSpeedX;
	int avgSpeedY = 0;
	
	int textTimeX;
	int textTimeY; 
	int timeX; 
	int timeY;
	
	int textDistanceX; 
	int textDistanceY;
	int distanceX;
	int distanceY;


	public TrackingMapOverlay(Drawable defaultMarker, LinkedList<TrackingPoint> trackingPoints,MapView mapView,  AssetManager asset) {
		super(defaultMarker);
		
		RallyeMaster.logDebug("Shows Overlay TrackingMapOverlay");
		this.trackingPoints = trackingPoints;
		mOverlays = new ArrayList<OverlayItem>();
		marker = defaultMarker;
		
		textPaint = new Paint();
		textPaint.setTextSize(16);
		
		
		backPaint = new Paint();
		backPaint.setARGB(150, 220, 220, 220);
		
		varPaint = new Paint();
		varPaint.setTextSize(24);
		lcdFont = Typeface.createFromAsset(asset,"font/lcdfont.tff");
		varPaint.setTypeface(lcdFont);
		textPaint.setFakeBoldText(false);
		circlePaint = new Paint();
		circlePaint.setARGB(200, 255, 255, 000);
		
		//Backgroud rectangles
		backRect1 = new RectF();
		backRect2 = new RectF();
		backRect3 = new RectF();
		backRect4 = new RectF();
		
		//Coordinates for the text
		txtSpeedX= 0;
		txtSpeedY= 0;
		speedX= 0;
		speedy= 0;
		
		txtAvgSpeedX = 0;
		txtAvgSpeedY = 0; 
		avgSpeedX = 0;
		avgSpeedY = 0;
		
		textTimeX= 0;
		textTimeY = 0; 
		timeX = 0; 
		timeY= 0;
		
		textDistanceX = 0; 
		textDistanceY = 0;
		distanceX = 0;
		distanceY = 0;
		
		calculateScreenPreferences(mapView);
		
		//perform all processing on a new ItemizedOverlay
		populate();
		
	}
	
	// calulates all rectance and font sizes for  the different screen sizes
	private void calculateScreenPreferences(MapView mapView){
		// Getting Screen Layout
		Configuration config = mapView.getResources().getConfiguration();
		int layout = config.screenLayout;
		RallyeMaster.logDebug("Layout: " +layout);		
	
		// Calculating Size of the Phone
		// I think emulator doesn't work right. couldn't get the right values
		// just tested with my normal size phone. hope it works with others too
		
		RallyeMaster.logDebug(" " + ((layout & Configuration.SCREENLAYOUT_SIZE_MASK)));
		if(((layout & Configuration.SCREENLAYOUT_SIZE_MASK)) == Configuration.SCREENLAYOUT_SIZE_LARGE ){
			RallyeMaster.logDebug("LARGE SCREEN");
			//TODO: make the size of the rect correct for each device
			backRect1 = new RectF(20 , 30,160,100);
			backRect2 = new RectF(20 ,130,160,200);
			backRect3 = new RectF(20 ,230,160,300);
			backRect4 = new RectF(20 ,330,160,400);
			
			txtSpeedX= 30;
			txtSpeedY= 50;
			speedX= 40;
			speedy= 80;
			
			txtAvgSpeedX = 30;
			txtAvgSpeedY = 150; 
			avgSpeedX = 40;
			avgSpeedY = 180;
			
			textTimeX= 30;
			textTimeY = 250; 
			timeX = 40; 
			timeY= 280;
			
			textDistanceX = 30; 
			textDistanceY = 350;
			distanceX = 40;
			distanceY = 380;
			
			textPaint.setTextSize(16);
			varPaint.setTextSize(24);
			
		}
		else if(((layout & Configuration.SCREENLAYOUT_SIZE_MASK)) == Configuration.SCREENLAYOUT_SIZE_NORMAL ){
			RallyeMaster.logDebug("NORMAL SCREEN");
			//TODO: make the size of the rect correct for each device
			backRect1 = new RectF(10, 20,140,80); // left, top , right , bottom
			backRect2 = new RectF(10 ,100,140,160);
			backRect3 = new RectF(10 ,180,140,240);
			backRect4 = new RectF(10 ,260,140,320);
			
			txtSpeedX= 20;
			txtSpeedY= 40;
			speedX= 30;
			speedy= 60;
			
			txtAvgSpeedX = 20;
			txtAvgSpeedY = 120; 
			avgSpeedX = 30;
			avgSpeedY = 140;
			
			textTimeX= 20;
			textTimeY = 200; 
			timeX = 30; 
			timeY= 220;
			
			textDistanceX = 20; 
			textDistanceY = 280;
			distanceX = 30;
			distanceY = 300;
			
			textPaint.setTextSize(12);
			varPaint.setTextSize(20);
		}
	}

	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		// TODO Auto-generated method stub
		return super.onTap(p, mapView);
	}


	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	 public void addOverlayItem(OverlayItem item) {
	   
	     marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
			boundCenter(marker);
			  item.setMarker(marker);
		 mOverlays.add(item);
		 //RallyeMaster.logDebug("OverlayItem added");
	     //RallyeMaster.logDebug("Size of OverlayItems: " + size());
	      populate();

	 }
 

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		
		String textSpeed;
		String textAvgSpeed;
		String textTime; 
		String textDistance;
		String speed;
		String avgSpeed;
		String time; 
		String distance; 
		
		if(trackingPoints.size() > 0 ){
			TrackingPoint lastTrackingPoint = trackingPoints.getLast();
			textSpeed = "Speed: " + " km/h";
			speed = ""+(int)lastTrackingPoint.getCurrentSpeed();
			textAvgSpeed = "Avg Speed: "+ " km/h";
			avgSpeed = ""+ (int)lastTrackingPoint.getAvgSpeed();
			textTime = "Time: ";
			time =  lastTrackingPoint.getTime().getHours() + ":" + lastTrackingPoint.getTime().getMinutes() + ":" + lastTrackingPoint.getTime().getSeconds();
			textDistance = "Distance: "  + "m";
			distance = ""+(int)lastTrackingPoint.getDistance();
		}else{
			textSpeed="Speed: km/h";
			speed = "0.0";
			textAvgSpeed ="Avg Speed: km/h";
			avgSpeed = "0.0";
			textTime = "Time: ";
			time = "00:00:00";
			textDistance = "Distance: m";
			distance = "0.0";
		}

		// Draw the marker    
		canvas.drawRoundRect(backRect1, 5, 5, backPaint);
		canvas.drawRoundRect(backRect2, 5, 5, backPaint);
		canvas.drawRoundRect(backRect3, 5, 5, backPaint);
		canvas.drawRoundRect(backRect4, 5, 5, backPaint);
		
		canvas.drawText(textSpeed, txtSpeedX,txtSpeedY, textPaint);
		canvas.drawText(speed, speedX,speedy, varPaint);
		
		canvas.drawText(textAvgSpeed, txtAvgSpeedX,txtAvgSpeedY , textPaint);
		canvas.drawText(avgSpeed, avgSpeedX,avgSpeedY, varPaint);
		
		canvas.drawText(textTime, textTimeX,textTimeY, textPaint);
		canvas.drawText(time, timeX,timeY, varPaint);
		
		canvas.drawText(textDistance, textDistanceX,textDistanceY , textPaint);
		canvas.drawText(distance, distanceX,distanceY, varPaint);

		super.draw(canvas, mapView, shadow);
		
	}
	
	

}
